package ar.posti.framework.query.hibernate;

import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

public class HibernateQueryFactory {
	private SessionFactory sessionFactory;
	
	public Query createQuery(String hql, Map<String, Object> variables) {
		Query query = currentSession().createQuery(hql);
		this.bindVariables(query, variables);

		return query;
	}

	public void bindVariables(Query query, Map<String, Object> variables) {
		for (String key : variables.keySet()) {
			Object value = variables.get(key);
			query.setParameter(key, value);
		}
	}

	private Session currentSession() {
		return sessionFactory.getCurrentSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
